<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="orderList">
      <el-table-column label="等级ID" align="center" prop="levelId" />
      <el-table-column label="等级名称" align="center" prop="levelName" />
      <!-- <el-table-column
        label="任意消费金额"
        align="center"
        prop="consumptionAmount"
      /> -->
      <el-table-column label="会员有效期(月)" align="center" prop="time">
        <template slot-scope="scope">
          <span>{{ scope.row.time }}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column
        label="购买指定产品(ID)"
        align="center"
        prop="buyProduct"
      /> -->
      <!-- <el-table-column
        label="推荐指定身份规则"
        align="center"
        prop="referrerLevel"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.referrerLevel">
            <div>推荐等级：{{ scope.row.referrerLevel.levelId }}</div>
            <div>推荐人数：{{ scope.row.referrerLevel.count }}</div>
          </div>
        </template>
      </el-table-column> -->
      <el-table-column label="佣金比例(%)" align="center" prop="discount">
        <template slot-scope="scope">
          <span>{{ Number(scope.row.discount) * 100 }}%</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="160px">
        <el-form-item label="等级名称" prop="levelName">
          <el-input v-model="form.levelName" placeholder="请输入等级名称" />
        </el-form-item>
        <el-form-item label="会员有效期(月)" prop="time">
          <el-input
            type="number"
            v-model="form.time"
            placeholder="请输入会员有效期(月)"
          />
        </el-form-item>
        <el-form-item label="佣金比例(%)" prop="discount">
          <el-input
            v-model="form.discount"
            type="number"
            :min="0"
            :max="100"
            placeholder="请输入佣金比例(%)"
            @blur="changeNum"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  productCateList,
  insertCate,
  detailCate,
  modifyCate,
  delCate,
} from "@/api/product/productCate";
import { userLevelList, changeUserLevelInfo } from "@/api/customer/customer";
import { getSassToken } from "@/utils/utils";
import { Debounce } from "@/utils/tools.js";
import imageDrapUpload from "@/components/Upload";
export default {
  name: "productCate",
  components: {
    imageDrapUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单表格数据
      orderList: [],
      typeList: [],
      // 弹出层标题
      title: "订单发货",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        cateName: "",
      },
      // 表单参数
      form: {
        levelId: null,
        discount: null,
        levelName: null,
        time: null,
      },
      // 表单校验
      rules: {
        levelName: [
          { required: true, message: "请输入等级名称", trigger: "blur" },
        ],
        time: [
          { required: true, message: "请输入会员有效期", trigger: "blur" },
        ],
        discount: [
          { required: true, message: "请输入佣金比例(1-100)", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    changeNum() {
      if (this.form.discount > 100) {
        this.form.discount = 100;
      }
      if (this.form.discount < 0) {
        this.form.discount = 0;
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      userLevelList(this.queryParams).then((response) => {
        response.data.records.forEach((item) => {
          if (item.referrerLevel && item.referrerLevel.length > 0) {
            item.referrerLevel = JSON.parse(item.referrerLevel);
          }
        });
        this.orderList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 删除按钮操作 */
    handleDelete: Debounce(function (row) {
      this.$modal
        .confirm('是否确认删除名称为"' + row.cateName + '"的数据项？')
        .then(function () {
          return delCate(row.cateId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    }, 500),
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate: Debounce(function (row) {
      this.reset();
      let data = JSON.parse(JSON.stringify(row));
      data.discount = Number(data.discount) * 100;
      this.form = data;
      this.open = true;
      this.title = "修改等级信息";
    }, 500),
    /** 提交按钮 */
    submitForm: Debounce(function (row) {
      let that = this;
      this.$modal
        .confirm("是否确认修改数据？")
        .then(function () {
          that.$refs["form"].validate((valid) => {
            if (valid) {
              let num = 0;
              if (that.form.discount > 0) {
                num = Number(that.form.discount) / 100;
              }
              let data = {
                levelName: that.form.levelName,
                time: that.form.time,
                discount: num,
                levelId: that.form.levelId,
              };
              if (that.form.time < 0) {
                that.$modal.msgError("请输入正确的会员有效期");
              } else {
                changeUserLevelInfo(data).then((response) => {
                  that.$modal.msgSuccess("修改成功");
                  that.open = false;
                  that.getList();
                });
              }
            }
          });
        })
        .catch(() => {});
    }, 500),
  },
};
</script>
<style lang="scss" scoped></style>
